package com.note.chh.notebook.UI.activity;

import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SPUtils;
import com.note.chh.notebook.Adapter.GuideViewPagerAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.MainActivity;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Widget.DotView;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *第一次 启动页面
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.vp_pager)
    ViewPager vpPager;
    @BindView(R.id.ll_dot)
    LinearLayout llDot;
    @BindView(R.id.btn_go)
    Button btnGo;
    private ArrayList<View> views;
    private DotView[] dotViews;
    private int currentIndex;


    @Override
    public void initView() {
        //隐藏状态栏
        statusBarVisible(false);
        //沉浸状态栏
//        QMUIStatusBarHelper.translucent(SplashActivity.this);
        //是否第一次进入app
//        if(SPUtils.getInstance(Constant.SP_NAME).getBoolean(Constant.KEY_FIRST_SPLASH,true)){
//            initSplashView();
//            initSplashListener();
//        } else {
            startActivity(GuideActivity.class);
            finish();
//        }
    }

    @Override
    protected void onDestroy() {
        //显示状态栏
        statusBarVisible(true);
        super.onDestroy();
    }

    @Override
    public void initData() {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_splash;
    }

    @OnClick(R.id.btn_go)
    public void onViewClicked() {
        SPUtils.getInstance(Constant.SP_NAME).put(Constant.KEY_FIRST_SPLASH,false);
        startActivity(MainActivity.class);
        finish();
    }


    private void initSplashView() {
        //实例化各个界面的布局对象
        View view1 = View.inflate(this, R.layout.activity_splash_view, null);
        View view2 = View.inflate(this, R.layout.activity_splash_view, null);
        View view3 = View.inflate(this, R.layout.activity_splash_view, null);
        View view4 = View.inflate(this, R.layout.activity_splash_view, null);

        ((ImageView) view1.findViewById(R.id.iv_image)).setImageResource(R.mipmap.pic_page_background);
        ((ImageView) view2.findViewById(R.id.iv_image)).setImageResource(R.mipmap.pic_page_background);
        ((ImageView) view3.findViewById(R.id.iv_image)).setImageResource(R.mipmap.pic_page_background);
        ((ImageView) view4.findViewById(R.id.iv_image)).setImageResource(R.mipmap.pic_page_background);

        views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
    }


    private void initSplashListener() {
        GuideViewPagerAdapter adapter = new GuideViewPagerAdapter(views);
        vpPager.setAdapter(adapter);
        vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentDot(position);
                if (position + 1 == vpPager.getAdapter().getCount()) {
                    if (btnGo.getVisibility() != View.VISIBLE) {
                        btnGo.setVisibility(View.VISIBLE);
                        btnGo.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
                    }
                } else {
                    if (btnGo.getVisibility() != View.GONE) {
                        btnGo.setVisibility(View.GONE);
                        btnGo.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_out));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        dotViews = new DotView[views.size()];
        for (int i = 0; i < views.size(); i++) {
            dotViews[i] = (DotView) llDot.getChildAt(i);
        }
        currentIndex = 0;
        dotViews[currentIndex].setIsSelected(true);
    }


    private void setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1 || currentIndex == position) {
            return;
        }
        dotViews[currentIndex].setIsSelected(false);
        dotViews[position].setIsSelected(true);
        currentIndex = position;
    }

    /**
     * 屏蔽返回键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
